﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellFileAttribute.h"
#include "CellString.h"
#include "CellStringConverter.h"

namespace cell
{

// 属性分隔符
#define ATTR_SEPARATOR "|"
#define ATTR_SEPARATOR_LEN 1

//---------------------------------------------------------------------------
FileAttribute::FileAttribute(void)
	: mExist(false)
	, mSize(0)
	, mLastModifyTime(0)
	, mHashCode("7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f")
{
}
//---------------------------------------------------------------------------
FileAttribute::~FileAttribute(void)
{
}
//---------------------------------------------------------------------------
FileAttribute& FileAttribute::operator =(const FileAttribute& other)
{
	mExist = other.mExist;
	mSize = other.mSize;
	mLastModifyTime = other.mLastModifyTime;
	mHashCode = other.mHashCode;
	return *this;
}
//---------------------------------------------------------------------------
bool FileAttribute::exist(void) const
{
	return mExist;
}
//---------------------------------------------------------------------------
bool FileAttribute::exist(void)
{
	return mExist;
}
//---------------------------------------------------------------------------
long FileAttribute::size(void) const
{
	return mSize;
}
//---------------------------------------------------------------------------
long FileAttribute::size(void)
{
	return mSize;
}
//---------------------------------------------------------------------------
const Date& FileAttribute::lastModifyTime(void) const
{
	return mLastModifyTime;
}
//---------------------------------------------------------------------------
const Date& FileAttribute::lastModifyTime(void)
{
	return mLastModifyTime;
}
//---------------------------------------------------------------------------
const String& FileAttribute::hashCode(void) const
{
	return mHashCode;
}
//---------------------------------------------------------------------------
const String& FileAttribute::hashCode(void)
{
	return mHashCode;
}
//---------------------------------------------------------------------------
int FileAttribute::serialize(char* out)
{
	// 序列化格式：是否存在|文件长度|最后修改日期|MD5码

	int cursor = 0;

	// 是否存在
	memcpy(out, mExist ? "1" : "0", 1);
	cursor += 1;

	if (mExist)
	{
		memcpy(out + cursor, ATTR_SEPARATOR, ATTR_SEPARATOR_LEN);
		cursor += ATTR_SEPARATOR_LEN;

		// 长度
		char szSize[32] = {0x0};
		sprintf(szSize, "%ld", mSize);
		memcpy(out + cursor, szSize, strlen(szSize));
		cursor += strlen(szSize);

		memcpy(out + cursor, ATTR_SEPARATOR, ATTR_SEPARATOR_LEN);
		cursor += ATTR_SEPARATOR_LEN;

		// 修改日期
		String strDate = Date::convertDateToString(mLastModifyTime);
		memcpy(out + cursor, strDate.c_str(), strDate.length());
		cursor += strDate.length();

		memcpy(out + cursor, ATTR_SEPARATOR, ATTR_SEPARATOR_LEN);
		cursor += ATTR_SEPARATOR_LEN;

		// 散列码
		memcpy(out + cursor, mHashCode.c_str(), mHashCode.length());
		cursor += mHashCode.length();
	}

	return cursor;
}
//---------------------------------------------------------------------------
void FileAttribute::deserialize(char* in, uint len)
{
	// 序列化格式：是否存在|文件长度|最后修改日期|MD5码

	String str(in);
	StringVector array = StringUtil::split(str, "|");
	mExist = StringConverter::parseBool(array[0]);

	if (mExist)
	{
		// 文件长度
		mSize = StringConverter::parseLong(array[1]);
		// 修改日期
		mLastModifyTime = Date::convertStringToDate(array[2]);
		// 散列码
		mHashCode = array[3];
	}
}
//---------------------------------------------------------------------------
void FileAttribute::exist(bool value)
{
	mExist = value;
}
//---------------------------------------------------------------------------
void FileAttribute::size(long value)
{
	mSize = value;
}
//---------------------------------------------------------------------------
void FileAttribute::lastModifyTime(const Date& date)
{
	mLastModifyTime = date;
}
//---------------------------------------------------------------------------
void FileAttribute::hashCode(const String& code)
{
	mHashCode = code;
}

} // end namespace cell
